﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MoetutuTranslate.Helper
{ /// <summary>
  /// 存放标签的结构体
  /// </summary>
    public struct TagStruct
    {
        String JpTag;
        String CnTag;
        public string JpTag1 { get => JpTag; set => JpTag = value; }
        public string CnTag1 { get => CnTag; set => CnTag = value; }
    }
    class TagHelper
    {

        public static List<string> HandleTag(List<string> GetList) {
            List<string> TempList = new List<string>();
            TempList = TagShort(GetList);
            
            Console.WriteLine("缩短tag"+TempList.Count);
            TempList= FileHelper.DeleteRepetText(TempList);
          
            Console.WriteLine("去空行："+TempList.Count);
            TempList = RemoveExcludeKey(TempList);
            Console.WriteLine("去垃圾关键词："+TempList.Count);
            return TempList;
        }
        public static List<string> RemoveExcludeKey(List<string> list)
        {

            List<string> TempList = new List<string>();
            foreach (string line in list)
            {

                if (StaticValue.ExcludeList.Contains(line.Trim()))
                    continue;
                else
                    TempList.Add(line.Trim());
            }
            return TempList;
        }

        /// <summary>
        /// 处理tag标签，告诉用户已经汉化
        /// </summary>
        /// <param name="JpTag"></param>
        /// <returns></returns>
        public static List<String> TranslateTagForText(List<String> JpTag)
        {
            List<String> TempTag = new List<string>();
            foreach (String line in JpTag)
            {

                String GetTag = SearchCnTag(line);

                if (GetTag != "")
                {  
                    TempTag.Add(line + string.Format("(已汉化：{0})", GetTag));
                    continue;
                }
                else
                {
                    TempTag.Add(line);
                    //   FileHelper.AppendUTF8Text(".\\english.txt", line);

                }
              
            }
            return TempTag;
        }
        /// <summary>
        /// 删除掉已经有汉化的标签。
        /// </summary>
        /// <param name="JpTag"></param>
        /// <returns></returns>
        public static List<string> RemoveCnTag(List<string> JpTag)
        {

            List<String> TempTag = new List<string>();
            foreach (String line in JpTag)
            {

                String GetTag = SearchCnTag(line);

                if (GetTag != "")
                {

                    continue;
                }
                else
                {
                    TempTag.Add(line);


                }

            }
            return TempTag;
        }
        /// <summary>
        /// 删除无用的标签
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public static string TagShort(string line)
        {
            line = line.Trim();
            line = TextHelper.ReplaceText(line, "[0-9]{2,9}users入り", "");
            line = TextHelper.ReplaceText(line, "Pixiv Id ", "");
            Console.WriteLine(line);
            return line;
        }
        /// <summary>
        /// 删除无用的标签
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public static List<string> TagShort(List<string> TagList)
        {
            List<string> list = new List<string>();
            foreach (string tag in TagList) {
                String line = tag;
               
                line = TagShort(line);
                Console.WriteLine("处理过"+line);
                list.Add(line);
            }
            return list;
        }
        /// <summary>
        /// 翻译标签
        /// </summary>
        /// <param name="JpTag"></param>
        /// <returns></returns>
        public static List<String> TranslateTag(List<String> JpTag)
        {
            List<String> TempTag = new List<string>();

            for (int i = 0; i < JpTag.Count; i++)
            {
                String Tag = TagShort(JpTag[i]);//先进行一次缩短
                String GetTag = SearchCnTag(Tag);//在进行汉化

                if (GetTag != "")
                {
                    if (!TempTag.Contains(GetTag))
                    {
                        Console.WriteLine("已汉化" + Tag);
                        TempTag.Add(GetTag);

                    }
                }
                else
                {

                    FileHelper.AppendUTF8Text(".\\english.txt", Tag);

                }
                if (!TempTag.Contains(Tag))
                    TempTag.Add(Tag);
            }

            foreach (string line in TempTag)
            {
                Console.WriteLine("已处理过：" + line);
            }
            return TempTag;
        }

        /// <summary>
        /// 读取xml信息方便汉化
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public static List<TagStruct> XmlReadAll(String FileName)
        {
            if (!FileHelper.FileExits(FileName))
                XmlHelper.CreateXML(FileName);
            List<TagStruct> TagList = new List<TagStruct>();

            XmlDocument xml = new XmlDocument();
            xml.LoadXml(FileHelper.ReadContextUtf8(FileName));
            foreach (XmlNode single in xml.SelectNodes("Root/Tag"))
            {

                try
                {

                    TagStruct TagTemp = new TagStruct();
                    
                    TagTemp.JpTag1 = single.SelectSingleNode("jptag").InnerText;
                    TagTemp.CnTag1 = single.SelectSingleNode("cntag").InnerText;
                    TagList.Add(TagTemp);
                }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                }
            }
            return TagList;
        }
        /// <summary>
        /// 读取jp的子列表
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public static List<String> XMLReadJp(List<TagStruct> TagStruct)
        {
            List<String> JpList = new List<string>();
            foreach (TagStruct Single in TagStruct)
            {

                JpList.Add(Single.JpTag1);
            }
            return JpList;

        }
        /// <summary>
        /// 查询中文标签
        /// </summary>
        /// <param name="JpTag"></param>
        /// <returns></returns>
        public static string SearchCnTag(String JpTag)
        {
            String text = "";
            IEnumerable<TagStruct> tagStruct = StaticValue.TagStruct.Where(x => x.JpTag1 == JpTag.Trim());
            foreach (TagStruct single in tagStruct)
            {
                Console.WriteLine("找到"+single.CnTag1);
                text = single.CnTag1;
            }
            return text;
        }
        /// <summary>
        /// 添加翻译进去
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="JpTagText"></param>
        /// <param name="CnTagText"></param>
        public static void XmlAdd(String FileName, String JpTagText, String CnTagText)
        {


            XmlHelper.CreateXML(FileName);

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(FileHelper.ReadContextUtf8(FileName));
            XmlNode xmldocSelect = xmlDoc.SelectSingleNode("Root");
            XmlElement Tags = xmlDoc.CreateElement("Tag");
            Tags.SetAttribute("Name", "TagList");
            XmlElement CnTag = xmlDoc.CreateElement("cntag");
            CnTag.InnerText = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(CnTagText));
            XmlElement JpTag = xmlDoc.CreateElement("jptag");
            JpTag.InnerText = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(JpTagText));
            Tags.AppendChild(JpTag);
            Tags.AppendChild(CnTag);
            xmldocSelect.AppendChild(Tags);
            xmlDoc.Save(FileName);
        }
    }
}
